package com.hrbxlh.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hrbxlh.actionImpl.*;
import com.hrbxlh.vo.SkillDTO;
import com.hrbxlh.pojo.SkillNotes;
import com.hrbxlh.util.*;
import com.hrbxlh.virtualization.pojo.ImageAllocation;
import com.hrbxlh.virtualization.service.ImageAllocationService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：CourseAction.java
* Description：课程action类
* History：
* Date           Author               Desc
* 2017.3.27		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class CourseAction {
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	@Autowired
	@Qualifier("chapterActionImpl")
	private ChapterActionImpl chapterActionImpl;
	
	@Autowired
	@Qualifier("myCourseActionImpl")
	private MyCourseActionImpl myCourseActionImpl;
	
	@Autowired
	@Qualifier("homeworkTestActionImpl")
	private HomeworkTestActionImpl homeworkTestActionImpl;
	
	@Autowired
	@Qualifier("departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;

	@Autowired
	@Qualifier("relationActionImpl")
	private RelationActionImpl relationActionImpl;

	@Autowired
	private ImageAllocationService imageAllocationService;

	/**
	 * @description 跳转到虚拟机页面(不使用)
	 * @param
	 * @return
	 * */
	@RequestMapping(value = "/skipEnvironment", method = RequestMethod.GET)
	public ModelAndView skipEnvironment(HttpServletRequest request,int courseId,int chapterId,int chapterNum)  {
		Map<String, Object> sessionMap = new HashMap<String, Object>();
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			sessionMap = new SessionValues().getSessionValues(new String[] {"roleId","stuorteaId"}, request, "userMsg");
			String[] keysArray = {"server_address", "project_name"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			sessionMap.putAll(urlmap);
			sessionMap.put("courseId",courseId);
			sessionMap.put("chapterId",chapterId);
			sessionMap.put("chapterNum",chapterNum);
			sessionMap.put("result", new ObjectMapper().writeValueAsString(sessionMap));
			return new ModelAndView("labEnvirments", sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("跳页异常skipEnvironment",e);
			return new ModelAndView("error", sessionMap);
		}
	}

	/**
	 * @description 登录跳转主页面。
	 * @param
	 * @return
	 * */
	@RequestMapping(value = "/skipMain", method = RequestMethod.GET)
	public ModelAndView skipMain(HttpServletRequest request)  {
		Map<String, Object> sessionMap = new HashMap<String, Object>();
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			sessionMap = new SessionValues().getSessionValues(new String[] {"roleId","stuorteaId","onlySid"}, request, "userMsg");
			sessionMap.put("result", new ObjectMapper().writeValueAsString(sessionMap));
			return new ModelAndView("index", sessionMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("跳页异常skipPage",e);
			return new ModelAndView("error", sessionMap);
		}
	}


	/**
	 * @description 带参数跳转想要去的页面
	 * @param jsonStr : pageName:页面名，带前面文件夹，paramData 参数数据
	 * @return  roleId 角色，stuorteaId 工号，paramData 参数
	 * */
	@RequestMapping(value = "/skipPage", method = RequestMethod.GET)
	public ModelAndView skipPage(String jsonStr, HttpServletRequest request)  {

		Map<String, Object> resultMap =courseActionImpl.getSkipPageMsg(request, jsonStr);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			String[] keysArray = {"tomcatUrl", "project_name","server_address"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String path = String.valueOf(urlmap.get("server_address"))+String.valueOf(urlmap.get("project_name"));
			resultMap.put("path",path);
			returnMap.put("result", new ObjectMapper().writeValueAsString(resultMap));

			return new ModelAndView((String)resultMap.get("pageName"), returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("跳页异常skipPage",e);
			returnMap.put("result", "fail");
			returnMap.put("failMsg", "跳转页面失败");
			return new ModelAndView("error", returnMap);
		}
	}

	/**
	 * @description 进页面要把登录人的名字带进来，赋值到教师栏，
					  带该教师所属的学院的全部专业列表，然后选择专业方向（下拉）
	 * @param request
	 * @param response
	 * @return  跳转到添加课程页面
	 */
	@RequestMapping(value = "/skipAddCourse", method = RequestMethod.POST)
	public void skipAddCourse(HttpServletRequest request, HttpServletResponse response)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"userName","stuorteaId"}, request, "userMsg");
			resultMap = sessionValues;
			//通过教师工号获取全部专业
			List<Map<String, Object>> professionList = courseActionImpl.getMyProfessionList(resultMap);
			resultMap.put("professionList", professionList);
			//用于查询预置进去的课程信息，不是预置的要给0
			resultMap.put("saveMark", 1);
			//获取选取的所有的课程以及章节的列表
			List<Map<String, Object>> courseList = courseActionImpl.getCourseList(resultMap);
			resultMap.put("courseList", courseList);
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("跳转添加课程页面出现异常skipAddCourse", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 添加课程   上半部分课程基本信息返回课程id
	 * @param jsonStr 课程信息：courseName,courseTitle,courseIntroduction,stuorteaId,courseColer,courseCredit,textSize,maxStu,titleSize,professionId,mainId
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addCourse", method = RequestMethod.POST)
	public void  addCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request, "userMsg");
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			//添加课程
			Map<String, Object> addResultMap = courseActionImpl.addCourse(paramMap);
			boolean flag = (Boolean)addResultMap.get("resultFlag");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", addResultMap.get("failMsg"));
			}else{
				//返回添加的课程id
				resultMap.put("courseId", ((Map<String, Object>)addResultMap.get("detailMsg")).get("courseId"));
				logger.info("添加课程,id为："+((Map<String, Object>)addResultMap.get("detailMsg")).get("courseId"));
			}
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("添加课程出现异常addCourse", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}



	/**
	 * @description 添加课程与章节的关联信息
	 * @param  jsonStr courseId
	 * @param jsonStr 章节列表信息 属性：chapterList  字段[{"chapterId":1,"chapterNum":1}...]
	 * */
	@RequestMapping(value = "/addCourseReChapters", method = RequestMethod.POST)
	public void  addCourseReChapters (HttpServletResponse response, HttpServletRequest request, String jsonStr)  {
	    courseActionImpl.addCourseReChapters(response,request, jsonStr);
	}

	
	/**
	 * @description 查询全部院系post
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getDepartmentListPost", method = RequestMethod.POST)
	public void  getDepartmentListPost (HttpServletResponse response)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//全部院系
			List<Map<String, Object>> departmentList = departMentActionImpl.getDepartmentAll(new HashMap<String, Object>());
			resultMap.put("departmentList", departmentList);
			logger.info("查询全部院系");
			resultMap.put("result","success");
		} catch (Exception e) {
			resultMap.put("result","fail");
			resultMap.put("failMsg","查询失败");
			e.printStackTrace();
			logger.error("查询全部院系出现异常getDepartmentListPost", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 查询非预置的课程（用于编辑课程信息）
	 * @param request 
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getAfterSaveChapter", method = RequestMethod.POST)
	public void  getAfterSaveChapter (HttpServletResponse response, HttpServletRequest request)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			resultMap.put("saveMark", 0);
			resultMap.put("releaseMark", 1);
			resultMap.put("shareMark",1);
			Map<String, Object> mapCollection = courseActionImpl.getNoInCourseList(resultMap, request);
			resultMap = mapCollection;
			resultMap.put("result", "success");
			logger.info("获取非预置信息,结果为："+resultMap.toString());
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("获取非预置信息出现异常getAfterSaveChapter", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**跳转到修改课程页面
	 * @description 进页面要把登录人的名字带进来，赋值到教师栏，
		带该教师所属的学院的全部专业列表，然后选择专业方向（下拉） 
	 * @param request
	 * @param response
	 * @return  跳转到修改课程页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveCourse", method = RequestMethod.POST)
	public void skipSaveCourse (String jsonStr, HttpServletRequest request, HttpServletResponse response) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取页面参数值
			Map<String,Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取课程信息
			Map<String, Object> courseDetail = courseActionImpl.getCourseDetail(preParamMap,request);
			//获取章节信息
			List<Map<String, Object>> chapterList = chapterActionImpl.getCourseChapter(preParamMap);
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"userName","stuorteaId"}, request, "userMsg");
			resultMap = sessionValues;
			//通过教师工号获取全部专业
			List<Map<String, Object>> professionList = courseActionImpl.getMyProfessionList(resultMap);
			resultMap.put("professionList", professionList);
			resultMap.put("course", courseDetail);
			resultMap.put("chapterList", chapterList);
			preParamMap.put("saveMark", 1);
			//获取选取的所有的课程以及章节的列表
			List<Map<String, Object>> courseList = courseActionImpl.getCourseList(preParamMap);
			resultMap.put("courseList", courseList);
			resultMap.put("result", "success");
			logger.info("跳转修改课程页面,参数为："+jsonStr);
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace(); 
			logger.error("跳转修改课程页面出现异常skipSaveCourse", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 修改课程  上半部分
	 * @param jsonStr courseId  +course的信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveCourse", method = RequestMethod.POST)
	public void  saveCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName()); 
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request, "userMsg");
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			Map<String, Object> saveResultMap = courseActionImpl.saveCourse(paramMap);
			boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!flag){
				resultMap.put("failMsg", saveResultMap.get("failMsg"));
			}
			logger.info("修改课程基本信息,参数为："+jsonStr);
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("修改课程基本信息出现异常saveCourse", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * @description 删除课程时判断是否与能力有关联
	 * @param jsonStr  List<String>形式   jsonStr = ["1","2","3"...]
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delCourseHivePower", method = RequestMethod.POST)
	public void  delCourseHivePower (HttpServletResponse response, String jsonStr)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
			Map<String,Object> returnMap =courseActionImpl.delCourseHivePower(paramList);
			boolean flag = (boolean) returnMap.get("resultFlag");
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("failMsg",returnMap.get("failMsg"));
			logger.info("删除课程,参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "fail");
			logger.error("删除课程时判断是否与能力有关联出现异常delCourse", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * @description 删除课程
	 * @param jsonStr  List<String>形式   jsonStr = ["1","2","3"...]
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delCourse", method = RequestMethod.POST)
	public void  delCourse (HttpServletResponse response, String jsonStr)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
			Map<String,Object> returnMap =courseActionImpl.delCourse(paramList);
			boolean flag = (boolean) returnMap.get("resultFlag");
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("failMsg",returnMap.get("failMsg"));
			logger.info("删除课程,参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "fail");
			logger.error("删除课程出现异常delCourse", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @description 获取删除课程的时候是否提示有人教授此课程的信息
	 * */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getDelCourseMark", method = RequestMethod.POST)
	public void  getDelCourseMark (HttpServletResponse response, String jsonStr) {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
			//查询这些课程是否有授课信息
			resultMap = relationActionImpl.getDelCourseMark(paramList);
		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("failMsg", "获取删除课程标志出现异常getDelCourseMark");
			e.printStackTrace();
			logger.error("获取删除课程标志出现异常getDelCourseMark", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 查询某章节名称是否存在(未使用)
	 * @param jsonStr  chapterName:
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getChapterExistMark", method = RequestMethod.POST)
	public void  getChapterExistMark (HttpServletResponse response, String jsonStr) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//获取参数值
			Map<String, Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取章节名称是否存在
			List<Map<String, Object>> list = chapterActionImpl.getChapterByName(paramMap);
			if(list != null && list.size() != 0){
				resultMap.put("result", "exists");
			}else{
				resultMap.put("result", "notexists");
			}
			logger.info("获取某章节，参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取章节出现异常getChapterExistMark", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**商莹    xzx后期修改
	 * @description 删除章节中的课件、作业、视频、备课材料、课后答案
	 * @param jsonStr   courseId  chapterNum type 1课件  2视频  3作业 5备课材料 6课后题， chapterId,
	 * @param response  
	 * @return  成功 失败
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delChapterCoHoVo", method = RequestMethod.POST)
	public void delChapterCoHoVo(HttpServletResponse response,String jsonStr)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //删除章节
			Map<String, Object> flag = chapterActionImpl.delChapterCoHoVo(paramMap);
			resultMark = ((Boolean)flag.get("result")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", flag.get("failMsg"));
			}
			logger.info("删除文件信息,参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除文件信息出现异常delChapterCoHoVo", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * @description 将课程变为不是选课的 或是选课的（未使用）
	 * @param jsonStr   courseId  chapterNum
	 * @param response  
	 * @return  成功 失败
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveCourseSelect", method = RequestMethod.POST)
	public void saveCourseSelect(String jsonStr, HttpServletResponse response)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {		
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //保存选课标识
			boolean flag = courseActionImpl.saveSelectMark(paramMap);
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存选课标识出现异常saveCourseSelect", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 跳转到错误页面
	 * @return
	 */
	@RequestMapping(value = "/skipErrorPage", method = RequestMethod.GET)
	public ModelAndView skipErrorPage (String jsonStr){
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			returnMap.put("failMsg", mapper.readValue(jsonStr, returnMap.getClass()).get("failMsg"));
			//页面名称
			pageName = "error";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("跳转错误页面出现异常skipErrorPage", e);
			pageName = "error";
		} finally {
			try {
				reJsonStr = mapper.writeValueAsString(returnMap);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			returnMap.put("result", reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 教师 课程管理  分页
	 * @param request
	 * @param response
	 * @return  跳转到课程管理
	 */
	@RequestMapping(value = "/skipTeaCourseMana", method = RequestMethod.POST)
	public void skipTeaCourseMana(HttpServletRequest request, HttpServletResponse response, String jsonStr) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			resultMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.getMapSpecialMark(response, mapper, resultMap, resultMap)) return;
			//获取登录人角色信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"roleId","stuorteaId"}, request, "userMsg");
			resultMap.put("roleId", sessionValues.get("roleId"));
			resultMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			resultMap.put("inMark", 1);
			//获取我的课程信息
			Map<String, Object> courseMap = myCourseActionImpl.getTeacherManaList(resultMap);
			if(courseMap != null){
				resultMap.put("courseList", courseMap.get("courseList"));
				resultMap.put("totalPage", courseMap.get("totalPage"));
				resultMap.put("pageNow", courseMap.get("pageNow"));
			}
			resultMap.put("result", "success");
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "fail");
			logger.error("跳转课程管理页面出现异常skipTeaCourseMana", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 进入结课管理列表
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipTeaEndClassList", method = RequestMethod.POST)
	public void skipTeaEndClassList(String jsonStr, HttpServletRequest request, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取登录人角色信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request, "userMsg");
			//获取教师的所有班级（从中间表里查）
			List<Map<String, Object>> classList = homeworkTestActionImpl.getTeaTeachClass(sessionValues);
			resultMap.put("classList", classList);
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			//获取该教师教的班级的课程信息
			Map<String, Object> classCourseMap = courseActionImpl.getMyClassCourseList(paramMap);
			if(classCourseMap != null){
				resultMap.put("classCourseList", classCourseMap.get("classCourseList"));
				resultMap.put("totalPage", classCourseMap.get("totalPage"));
				resultMap.put("pageNow", classCourseMap.get("pageNow"));
			}
			//返回条件
			resultMap.put("pageSize", paramMap.get("pageSize"));
			resultMap.put("classId", paramMap.get("classId"));
			resultMap.put("classEndMark", paramMap.get("classEndMark"));
			resultMap.put("result", "success");
			logger.info("跳转结课管理页面");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("跳转结课管理页面出现异常skipTeaEndClassList", e);
			resultMap.put("result", "fail");
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @description 点击结课，进行结课操作，参数是班级id，课程id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/endCourseClass", method = RequestMethod.POST)
	public void  endCourseClass (HttpServletResponse response, HttpServletRequest request, String jsonStr)   {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//结课
			Map<String,Object> returnMap = courseActionImpl.endCourseClass(paramMap,request);
			boolean saveResult = Boolean.parseBoolean(String.valueOf(returnMap.get("result")));
			//返回参数
			resultMark = (saveResult) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("failMsg", returnMap.get("failMsg"));
			logger.info("结课，参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("结课出现异常endCourseClass", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 获取某个老师教的全部课程
	 * @param jsonStr   需要老师的id
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getMyClasses", method = RequestMethod.POST)
	public void  getMyClasses (HttpServletResponse response, String jsonStr)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			List<Map<String, Object>> courseList = courseActionImpl.getMyClasses(paramMap);
			resultMap.put("courseList", courseList);
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("获取教师教的课程出现异常getMyClasses", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @description  批量下载课件等信息
	 * @param request  jsonStr ={"courseId":课程id，"downloadType":下载类型：1课件，2实验手册，3备课材料，4作业及作业答案 5视频 6 课程材料}
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/downloadCourseFile", method = RequestMethod.POST)
	public void downloadCourseFile(String jsonStr, HttpServletRequest request, HttpServletResponse response) {
		courseActionImpl.downloadCourseFileFunc(jsonStr, request, response);
	}

	/**
	 * @description 通过课程id获取这个课什么类型的课件不能下载
	 * */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getDownloadMark", method = RequestMethod.POST)
	public void getDownloadMark(String jsonStr , HttpServletResponse response) {
		courseActionImpl.getCantLoadMark(jsonStr, response);
	}

	/**
	 * @description  通过教师id和课程id获取课程简介，教师简介，
	 * @param  jsonStr = {"stuorteaId":教师id，"courseId"：课程id}
	 * */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getIntroctionByIds", method = RequestMethod.POST)
	public void getIntroctionByIds(String jsonStr, HttpServletRequest request, HttpServletResponse response)  {
		courseActionImpl.getIntroctionByIds(jsonStr, request, response);
	}

	/**
	 * @description 将虚拟机进页面需要的参数存入session（未使用）
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveEnvironmentParam", method = RequestMethod.POST)
	public void saveEnvironmentParam(HttpServletResponse response, String jsonStr, HttpSession session)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			Map<String, Object> sessionMap = new HashMap<String, Object>();
			sessionMap.put("environmentParam", jsonStr);
//			byte[] encryptMsg = CodeConvertUtil.encryptCode(new ObjectMapper().writeValueAsString(sessionMap));
			session.setAttribute("environmentParam", jsonStr);
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("将虚拟机进页面需要的参数保存出现异常saveEnvironmentParam", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @description 从session中获取虚拟机参数（未使用）
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getEnvironmentParam", method = RequestMethod.POST)
	public void getEnvironmentParam(HttpServletRequest request, HttpServletResponse response)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			SessionValues sessionValues = new SessionValues();
			Map<String, Object> environmentParamMap = sessionValues.getSessionValues(new String[] {"environmentParam"}, request, "environmentParam");
			resultMap.putAll(environmentParamMap);
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("将虚拟机进页面需要的参数保存出现异常saveEnvironmentParam", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * sy
	 * @description 发布课程
	 * @param jsonStr list[courseId]
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/releaseCourse", method = RequestMethod.POST)
	public void  releaseCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			Map<String, Object> saveResultMap = courseActionImpl.releaseCourse(paramMap);
			boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("failMsg", saveResultMap.get("failMsg"));
			if(Integer.parseInt(String.valueOf(paramMap.get("releaseMark")))==1){
				//当时发布课程时，调用王晨的方法
				List<Integer> courseList = (List<Integer>) paramMap.get("list");
				imageAllocationService.release(courseList.get(0));
			}

			logger.info("发布课程基本信息,参数为："+jsonStr);
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("发布课程基本信息出现异常releaseCourse", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * sy
	 * @description 分享课程
	 * @param jsonStr list[courseId]
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/shareCourse", method = RequestMethod.POST)
	public void  shareCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr)  {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			Map<String, Object> saveResultMap = courseActionImpl.shareCourse(paramMap,request);
			boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("failMsg", saveResultMap.get("failMsg"));
			logger.info("分享课程基本信息,参数为："+jsonStr);
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("发布课程基本信息出现异常releaseCourse", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}

	@PostMapping("/skill/model-import")
	@ResponseBody
	public Result importSkillExcel(
			@RequestParam(value = "file")  MultipartFile file,
			@RequestParam(value = "skillId", required = false) Integer skillId,
			@RequestParam(value = "role", required = false) Integer role) {

		// 解析excel文件
		List<SkillDTO> allByExcel = ExcelsUtil.getAllByExcel(
				file, 0, 1);
		if (CollectionUtils.isEmpty(allByExcel)) {
			return Result.error(ResultEnum.SERVER_ERROR, "excel暂无添加数据，请重新上传");
		}
		//入库
		return Result.success(courseActionImpl.insertSkill(allByExcel, skillId, file, role));
	}

	@RequestMapping(value = "/querySkillInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result queryClassInfo(@RequestParam Integer skillId,HttpServletResponse response, HttpServletRequest request) {
		List<SkillNotes> list = courseActionImpl.querySkill(skillId);
		return Result.success(list);
	}

	@GetMapping(value = "/updateSkillInfo")
	@ResponseBody
	public Result updateSkillInfo(@RequestParam Integer skillId,
								  @RequestParam String name) {
		courseActionImpl.updateSkill(skillId, name);
		return Result.success();
	}

	@GetMapping(value = "/querySkillOther")
	@ResponseBody
	public Result querySkillOther(@RequestParam(value = "skillId", required = false) Integer skillId,HttpServletResponse response, HttpServletRequest request) {
		Map<String, List<SkillNotes>> list = courseActionImpl.querySkillOther(skillId);
		return Result.success(list);
	}
}